package com.genius.service.impl;

import com.genius.annotation.PreDataAuth;
import com.genius.constant.DataStatusConstants;
import com.genius.enums.ResponseType;
import com.genius.exception.GraceException;
import com.genius.mapper.DeptMapper;
import com.genius.service.DeptService;
import com.genius.system.pojo.Dept;
import com.genius.utils.TreeUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

@Slf4j
@Service
public class DeptServiceImpl implements DeptService {

    @Autowired
    private DeptMapper deptMapper;

    @Override
    @PreDataAuth(deptAlias = "d", userAlias = "u")
    public List<Dept> selectAllDept(Dept dept) {
        List<Dept> deptList = deptMapper.selectAllDept(dept);
        if(!deptList.isEmpty()){
            return buildDept(deptList);
        }
        return new ArrayList<>();
    }

    @Override
    @Transactional
    public int insertDept(Dept dept) {
        Dept parentDept = deptMapper.selectDeptById(dept.getParentId());
        // 如果父节点不为正常状态,则不允许新增子节点
        if(parentDept == null){
            dept.setAncestors("0");
        } else if (!DataStatusConstants.NORMAL.equals(parentDept.getStatus())) {
            GraceException.display(ResponseType.DATA_DISABLE);
        } else {
            dept.setAncestors(parentDept.getAncestors() + "," + dept.getParentId());
        }
        return deptMapper.insertDept(dept);
    }

    @Override
    @Transactional
    public int updateDept(Dept dept) {
        Dept newParentDept = deptMapper.selectDeptById(dept.getParentId());
        Dept oldDept = deptMapper.selectDeptById(dept.getDeptId());
        if (newParentDept != null && oldDept !=null) {
            String newAncestors = newParentDept.getAncestors() + "," + newParentDept.getDeptId();
            String oldAncestors = oldDept.getAncestors();
            dept.setAncestors(newAncestors);
            updateDeptChildren(dept.getDeptId(), newAncestors, oldAncestors);
        }
        return deptMapper.updateDept(dept);
    }

    @Override
    public boolean hasChildByDeptId(Long deptId) {
        return deptMapper.hasChildByDeptId(deptId);
    }

    @Override
    @Transactional
    public int deleteDeptById(Long deptId) {
        return deptMapper.deleteDeptById(deptId);
    }

    @Override
    public boolean selectDeptIsExistByDeptIds(Long[] deptIds) {
        return deptMapper.selectDeptIsExistByDeptIds(deptIds);
    }

    private List<Dept> buildDept(List<Dept> deptList){
        List<Dept> deptTree = new ArrayList<>();
        try{
            deptTree.addAll(TreeUtils.buildTree(deptList, "deptId"));
        }catch(Exception e){
            e.printStackTrace();
        }
        return deptTree;
    }

    /**
     * 修改子元素关系
     *
     * @param deptId 被修改的部门ID
     * @param newAncestors 新的父ID集合
     * @param oldAncestors 旧的父ID集合
     */
    private void updateDeptChildren(Long deptId, String newAncestors, String oldAncestors) {
        List<Dept> children = deptMapper.selectDeptChildrenById(deptId);
        for (Dept child : children) {
            child.setAncestors(child.getAncestors().replaceFirst(oldAncestors, newAncestors));
        }
        if (children.size() > 0) {
            deptMapper.updateDeptChildren(children);
        }
    }
}
